<?php
class Admin_ProductCategoryImageController extends Apps_Controller_Action_Admin {

    public function indexAction() {
        $rq = $this->_request;

        $pcid = $rq->getParam('pcid');

        if($rq->getParam('act_delete_gallery')) {
            $this->_forward('delete-gallery', null, null);
        }

        $tbPCImage = new Model_DbTable_ProductCategoryImage();
        $dThumbnail	= $tbPCImage->fetchAll(array('product_category_id =?' => $pcid));

        //Zend_Debug::dump($dThumbnail);die;

        if ($rq->isPost()) {

            $ids            = $this->_request->getParam('id');
            $linkTos        = $this->_request->getParam('link_to');
            $captions       = $this->_request->getParam('caption');
            $sortingNumbers = $this->_request->getParam('sorting_number');

            if(is_array($ids)) {
                foreach ($ids as $k => $id) {
                    $tbPCImage->safelyUpdate(array('sorting_number' => $sortingNumbers[$k],
                            'link_to' 		=> $linkTos[$k],
                            'caption'		=> $captions[$k]),
                            array('id =? ' => $id));
                }
            }

            if(Apps_Plugin_Upload_File::isUploaded('thumbnail_up')) {
                $uploader = new Apps_Plugin_Upload_File();
                $uploader->setMaxQuantity(1)
                ->setSubCategory(Apps_Plugin_Upload_File::CAT_GALLERY);

                $thumbnail = ($uploaded = $uploader->upload())
                ? $uploaded['thumbnail_up']['uri']
                : null;

                $tbPCImage->safelyInsert(array('product_category_id' => $pcid,
                                               'path' 			     => $thumbnail,
                                               'sorting_number'      => $rq->getParam('sorting_number_up'),
                                               'caption'		     => $rq->getParam('caption_up')));

                //resize images
                try {
                    $image = new Apps_Plugin_ResizeImages();
                    $image->load(PUBLIC_PATH .$thumbnail);
                    $image->resize(930, 250);
                    $image->save(PUBLIC_PATH .$thumbnail);
                } catch (Exception $e) {
                    //Excetion here
                }
            }

            // Delete
            $ids = $rq->getParam('item_checker');
            if($ids) {
                $tbGallery = new Model_DbTable_ProductCategoryImage();
                foreach($ids as $v) {
                    $gallery = $tbGallery->fetchRow(array('id =?' => $v));
                    if($gallery) {
                        $tbGallery->delete(array('id =?' => $v));
                        $this->deletePhoto($gallery['path']);
                    }
                }
            }

            $this->_redirect('/admin/product-category-image/index/pcid/' . $pcid);
        }
        $this->view->dataPhoto 	= $dThumbnail;
    }
}